package org.bee.service;

import jakarta.annotation.Resource;
import org.bee.enums.IsDelete;
import org.bee.input.DictSearchVo;
import org.bee.mappers.DictMapper;
import org.bee.model.Dict;
import org.bee.model.DictExample;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 陈国松
 * @date 2024-09-29 23:22:13
 * @description 字典表Service层
 */
@Service
public class DictService{
    @Resource
    private DictMapper dictMapper;

    /**
     * 插入字典表
     * @param dict 字典表实体
     * @return 插入结果
     */
    public int insert(Dict dict) {
        return dictMapper.insert(dict);
    }

    /**
     * 删除字典表
     * @param dictId 字典表id
     * @return 删除结果
     */
    public int delete(String dictId,Boolean isDelete) {
        if(isDelete){
            return dictMapper.deleteByPrimaryKey(dictId);
        }else{
            Dict dict = new Dict();
            dict.setId(dictId);
            dict.setIsDeleted(true);
            return dictMapper.updateByPrimaryKeySelective(dict);
        }
    }

    /**
     * 根据主键更新字典表
     * @param dict 字典表实体
     * @return 更新结果
     */
    public int update(Dict dict) {
        return dictMapper.updateByPrimaryKeySelective(dict);
    }

    /**
    * 查询字典表
    * @param dictSearchVo 字典表查询参数
    * @return 字典表列表
    */
    public List<Dict> select(DictSearchVo dictSearchVo) {
        DictExample dictExample = new DictExample();
        DictExample.Criteria criteria = dictExample.createCriteria();
        if (dictSearchVo.getDictCode() != null) {
                criteria.andDictCodeLike("%"+dictSearchVo.getDictCode()+"%");
        }
        if (dictSearchVo.getTableName() != null) {
                criteria.andTableNameLike("%"+dictSearchVo.getTableName()+"%");
        }
        criteria.andIsDeletedEqualTo(IsDelete.FALSE.getCode());
        return dictMapper.selectByExample(dictExample);
    }

    /**
     * 根据主键查询字典表
     * @param id 字典表id
     * @return 字典结果
     */
    public Dict selectById(String id) {
        return dictMapper.selectByPrimaryKey(id);
    }
}